package main

import (
	"ball/api"
	"ball/api/handler"
	"ball/global"
	"ball/inits"
	"ball/internal/model"
	"ball/internal/queue"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"log"
)

func main() {
	inits.InitMysql()
	inits.InitRabbitMq()
	r := gin.Default()
	go all()
	api.Router(r)
	r.Run("localhost:8080")
}

func all() {

	msgs := queue.PublishSub("notification")

	forever := make(chan bool)
	//启用协程处理消息
	go func() {
		for d := range msgs {
			//消息逻辑处理，可以自行设计逻辑]
			log.Printf("Received a message: %s", d.Body)
			var chat model.Chats
			json.Unmarshal(d.Body, &chat)
			for _, conn := range global.OnlineUser {
				handler.WSRespSuccess(conn, chat)
			}

		}
	}()

	log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
	<-forever
}
